import django
from django import http
from django import shortcuts

from google.appengine.api import images

from blog.models import *
from blog.helper import *
from blog.sql import *
from blog.sessions import *
from conf.settings import *
 
def list(request):
    params = {}
    where = ""
    now = datetime.datetime.now()
    
    cate_id = SafeGet('cate_id', None, request)
    reload = int(SafeGet('reload', 10, request))
    if reload < 2:
        reload = 2
    params['reload'] = reload
    params['Title'] = 'List Chat' 
    params['minDate'] = ''
    return respond(request, None, 'vchat_list', params)

def list_ajax(request):
    params = {}
    where = ""
    now = datetime.datetime.now()
    
    cate_id = SafeGet('cate_id', None, request)
    minDate = SafeGet('minDate', '', request)
    
    if cate_id:
        vArticle = VArticle.get_by_id(int(cate_id)) 
       
    if minDate == '':
        items = VChat.gql("ORDER BY created DESC LIMIT 20")
    else:
        minDate = DateTimeRealToDB(minDate)
        items = VChat.gql("WHERE created > :1 ORDER BY created DESC LIMIT 20",minDate)
    
    minDate = DateTimeRealToDB('2009-1-1 1:1:1.1')
    if items.count()>0:
        for item in items:
            if item.created > minDate:
                minDate = item.created
    else:
        minDate = now       
    params['items'] = items
    params['minDate'] = minDate
    return respond(request, None, 'vchat_list_ajax', params)

def listComment(request):
    params = {}
    where = ""
    now = datetime.datetime.now()
    
    article_id = SafeGet('article_id', None, request)
    minDate = SafeGet('minDate', '', request)
    
    if article_id:
        vArticle = VArticle.get_by_id(int(article_id)) 
       
    if minDate == '':
        if article_id is None:
            items = VChat.gql("ORDER BY created DESC LIMIT 20")
        else:
            cacheItem = "chat"+article_id
            items = memcache.get(cacheItem)
            if items is None:
                items = vArticle.chat.order('-created')
                items = items.fetch(50)
                memcache.set(cacheItem, items, 2)
            params['items'] = items
            return respond(request, None, 'vchat_list_comment', params)
    else:
        minDate = DateTimeRealToDB(minDate)
        items = VChat.gql("WHERE created > :1 ORDER BY created DESC LIMIT 20",minDate)
    
    minDate = DateTimeRealToDB('2009-1-1 1:1:1.1')
    if items.count()>0:
        for item in items:
            if item.created > minDate:
                minDate = item.created
    else:
        minDate = now       
    params['items'] = items
    params['minDate'] = minDate
    return respond(request, None, 'vchat_list_comment', params)

def delete(request, idItem):
    member = Permission(1)
    item = None
    if idItem:
        item = VChat.get_by_id(int(idItem))
        if item is None:
            return http.HttpResponseNotFound('No Item')
        else:
            if isAdmin() is None:
                if item.member.user != member.user:
                    return respond(request, None, 'message_box', {"message":"No Permission."})
    item.delete()
    return http.HttpResponseRedirect('/user/vchat')

def add(request):
    return edit(request)

def edit(request):
    member = Permission(1)
    if member is None:
        return http.HttpResponse("login")
    result = "ok"
    if member is None:
        result = "error"
    idItem = SafePost('idItem',None, request)
   
    item = None
    
    if idItem:
        item = VChat.get_by_id(int(idItem))
        if item is None:
            result = "error"
        else:
            if item.member.user != member.user:
                result = "error"
    
    if result =="error":
        return http.HttpResponse(result)
    
    if item is None:
        item = VChat()
    try:            
        item.scontent   = unicode(SafePost('scontent', '', request), 'utf-8')
        item.member     = member
        item.member_name= member.name 
        item.member_id  = member.key().id()
        article_id = SafePost('article_id', None, request)
        if article_id:
            vArticle    = VArticle.get_by_id(int(article_id))
            if vArticle:
                item.article_id= int(article_id)
                item.article   = vArticle
            
    except db.Error, err:
        print err
        result = "error"
    
    if result =="ok":
        item.put()

    return http.HttpResponse(result)

def view(request, idItem):
    params = {}
    params['Title'] = 'View Article'
    
    item = None
    if idItem:
        item = VChat.get_by_id(int(idItem))
        if item:
            params['item'] = item
            params['Title'] = item.title
            item.hit = item.hit + 1
            item.put()
            return respond(request, None, 'vchat_view', params)
    
    return respond(request, None, 'message_box', {"message":"No Article."})

def image(request,idItem):
    if idItem:
        item= VChat.get_by_id(int(idItem))
        if item.image:
            return http.HttpResponse(item.image,mimetype='image/png')
    return  http.HttpResponse('')
